package com.example.heimashop.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 数据库帮助类
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "heimashop.db";
    private static final int DATABASE_VERSION = 2;

    // 购物车表
    public static final String TABLE_CART = "cart";
    public static final String COLUMN_CART_ID = "id";
    public static final String COLUMN_GOODS_ID = "goods_id";
    public static final String COLUMN_GOODS_NAME = "goods_name";
    public static final String COLUMN_GOODS_PRICE = "goods_price";
    public static final String COLUMN_GOODS_IMAGE = "goods_image";
    public static final String COLUMN_GOODS_COUNT = "goods_count";
    public static final String COLUMN_IS_CHECKED = "is_checked";

    // 收货地址表
    public static final String TABLE_ADDRESS = "address";
    public static final String COLUMN_ADDRESS_ID = "id";
    public static final String COLUMN_CONSIGNEE = "consignee";
    public static final String COLUMN_PHONE = "phone";
    public static final String COLUMN_PROVINCE = "province";
    public static final String COLUMN_CITY = "city";
    public static final String COLUMN_DISTRICT = "district";
    public static final String COLUMN_DETAIL_ADDRESS = "detail_address";
    public static final String COLUMN_IS_DEFAULT = "is_default";

    // 订单表
    public static final String TABLE_ORDER = "orders";
    public static final String COLUMN_ORDER_ID = "id";
    public static final String COLUMN_ORDER_NO = "order_no";
    public static final String COLUMN_ORDER_CONSIGNEE = "consignee";
    public static final String COLUMN_ORDER_PHONE = "phone";
    public static final String COLUMN_ORDER_ADDRESS = "address";
    public static final String COLUMN_TOTAL_PRICE = "total_price";
    public static final String COLUMN_STATUS = "status";
    public static final String COLUMN_CREATE_TIME = "create_time";

    // 订单商品表
    public static final String TABLE_ORDER_GOODS = "order_goods";
    public static final String COLUMN_ORDER_GOODS_ID = "id";
    public static final String COLUMN_ORDER_GOODS_ORDER_ID = "order_id";
    public static final String COLUMN_ORDER_GOODS_GOODS_ID = "goods_id";
    public static final String COLUMN_ORDER_GOODS_NAME = "goods_name";
    public static final String COLUMN_ORDER_GOODS_PRICE = "goods_price";
    public static final String COLUMN_ORDER_GOODS_IMAGE = "goods_image";
    public static final String COLUMN_ORDER_GOODS_COUNT = "goods_count";

    private static DatabaseHelper instance;

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static synchronized DatabaseHelper getInstance(Context context) {
        if (instance == null) {
            instance = new DatabaseHelper(context.getApplicationContext());
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建购物车表
        String createCartTable = "CREATE TABLE " + TABLE_CART + " (" +
                COLUMN_CART_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_GOODS_ID + " INTEGER NOT NULL, " +
                COLUMN_GOODS_NAME + " TEXT NOT NULL, " +
                COLUMN_GOODS_PRICE + " REAL NOT NULL, " +
                COLUMN_GOODS_IMAGE + " TEXT, " +
                COLUMN_GOODS_COUNT + " INTEGER NOT NULL DEFAULT 1, " +
                COLUMN_IS_CHECKED + " INTEGER NOT NULL DEFAULT 1)";
        db.execSQL(createCartTable);

        // 创建收货地址表
        String createAddressTable = "CREATE TABLE " + TABLE_ADDRESS + " (" +
                COLUMN_ADDRESS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_CONSIGNEE + " TEXT NOT NULL, " +
                COLUMN_PHONE + " TEXT NOT NULL, " +
                COLUMN_PROVINCE + " TEXT NOT NULL, " +
                COLUMN_CITY + " TEXT NOT NULL, " +
                COLUMN_DISTRICT + " TEXT NOT NULL, " +
                COLUMN_DETAIL_ADDRESS + " TEXT NOT NULL, " +
                COLUMN_IS_DEFAULT + " INTEGER NOT NULL DEFAULT 0)";
        db.execSQL(createAddressTable);

        // 创建订单表
        String createOrderTable = "CREATE TABLE " + TABLE_ORDER + " (" +
                COLUMN_ORDER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_ORDER_NO + " TEXT NOT NULL UNIQUE, " +
                COLUMN_ORDER_CONSIGNEE + " TEXT NOT NULL, " +
                COLUMN_ORDER_PHONE + " TEXT NOT NULL, " +
                COLUMN_ORDER_ADDRESS + " TEXT NOT NULL, " +
                COLUMN_TOTAL_PRICE + " REAL NOT NULL, " +
                COLUMN_STATUS + " TEXT NOT NULL, " +
                COLUMN_CREATE_TIME + " INTEGER NOT NULL)";
        db.execSQL(createOrderTable);

        // 创建订单商品表
        String createOrderGoodsTable = "CREATE TABLE " + TABLE_ORDER_GOODS + " (" +
                COLUMN_ORDER_GOODS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_ORDER_GOODS_ORDER_ID + " INTEGER NOT NULL, " +
                COLUMN_ORDER_GOODS_GOODS_ID + " INTEGER NOT NULL, " +
                COLUMN_ORDER_GOODS_NAME + " TEXT NOT NULL, " +
                COLUMN_ORDER_GOODS_PRICE + " REAL NOT NULL, " +
                COLUMN_ORDER_GOODS_IMAGE + " TEXT, " +
                COLUMN_ORDER_GOODS_COUNT + " INTEGER NOT NULL)";
        db.execSQL(createOrderGoodsTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级时添加新表
        if (oldVersion < 2) {
            // 创建订单表
            String createOrderTable = "CREATE TABLE " + TABLE_ORDER + " (" +
                    COLUMN_ORDER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_ORDER_NO + " TEXT NOT NULL UNIQUE, " +
                    COLUMN_ORDER_CONSIGNEE + " TEXT NOT NULL, " +
                    COLUMN_ORDER_PHONE + " TEXT NOT NULL, " +
                    COLUMN_ORDER_ADDRESS + " TEXT NOT NULL, " +
                    COLUMN_TOTAL_PRICE + " REAL NOT NULL, " +
                    COLUMN_STATUS + " TEXT NOT NULL, " +
                    COLUMN_CREATE_TIME + " INTEGER NOT NULL)";
            db.execSQL(createOrderTable);

            // 创建订单商品表
            String createOrderGoodsTable = "CREATE TABLE " + TABLE_ORDER_GOODS + " (" +
                    COLUMN_ORDER_GOODS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_ORDER_GOODS_ORDER_ID + " INTEGER NOT NULL, " +
                    COLUMN_ORDER_GOODS_GOODS_ID + " INTEGER NOT NULL, " +
                    COLUMN_ORDER_GOODS_NAME + " TEXT NOT NULL, " +
                    COLUMN_ORDER_GOODS_PRICE + " REAL NOT NULL, " +
                    COLUMN_ORDER_GOODS_IMAGE + " TEXT, " +
                    COLUMN_ORDER_GOODS_COUNT + " INTEGER NOT NULL)";
            db.execSQL(createOrderGoodsTable);
        }
    }
}
